package supplier

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/area"
	"github.com/flipped-aurora/gin-vue-admin/server/model/supplier"
	supplierReq "github.com/flipped-aurora/gin-vue-admin/server/model/supplier/request"
	"gorm.io/gorm"
)

type LsSupplierService struct{}

// CreateLsSupplier 创建lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) CreateLsSupplier(lsSupplier *supplier.LsSupplier) (err error) {
	err = global.GVA_DB.Create(lsSupplier).Error
	return err
}

// DeleteLsSupplier 删除lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) DeleteLsSupplier(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&supplier.LsSupplier{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&supplier.LsSupplier{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsSupplierByIds 批量删除lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) DeleteLsSupplierByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&supplier.LsSupplier{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&supplier.LsSupplier{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsSupplier 更新lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) UpdateLsSupplier(lsSupplier supplier.LsSupplier) (err error) {
	err = global.GVA_DB.Model(&supplier.LsSupplier{}).Where("id = ?", lsSupplier.ID).Updates(&lsSupplier).Error
	return err
}

// GetLsSupplier 根据ID获取lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) GetLsSupplier(ID string) (lsSupplier supplier.LsSupplier, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsSupplier).Error
	lsSupplier.Province = GetAreaInfoByParams(lsSupplier.Province)
	lsSupplier.City = GetAreaInfoByParams(lsSupplier.City)
	lsSupplier.Area = GetAreaInfoByParams(lsSupplier.Area)
	return
}
func GetAreaInfoByParams(code string) string {
	db := global.GVA_DB.Model(&area.LsArea{})
	var area area.LsArea
	db.Where("adcode=?", code).Find(&area)
	return area.Name
}

// GetLsSupplierInfoList 分页获取lsSupplier表记录
// Author [yourname](https://github.com/yourname)
func (lsSupplierService *LsSupplierService) GetLsSupplierInfoList(info supplierReq.LsSupplierSearch) (list []supplier.LsSupplier, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&supplier.LsSupplier{})
	var lsSuppliers []supplier.LsSupplier
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.SupplierCode != nil && *info.SupplierCode != "" {
		db = db.Where("supplier_code = ?", *info.SupplierCode)
	}
	if info.SupplierName != nil && *info.SupplierName != "" {
		db = db.Where("supplier_name = ?", *info.SupplierName)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["sort"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&lsSuppliers).Error
	for k := range lsSuppliers {
		lsSuppliers[k].Province = GetAreaInfoByParam(lsSuppliers[k].Province)
		lsSuppliers[k].City = GetAreaInfoByParam(lsSuppliers[k].City)
		lsSuppliers[k].Area = GetAreaInfoByParam(lsSuppliers[k].Area)
	}
	return lsSuppliers, total, err
}
func GetAreaInfoByParam(code string) string {
	db := global.GVA_DB.Model(&area.LsArea{})
	var area area.LsArea
	db.Where("adcode=?", code).Find(&area)
	return area.Name
}
func (lsSupplierService *LsSupplierService) GetLsSupplierPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}

func (lsSupplierService *LsSupplierService) GetLsAreaList() (interface{}, interface{}) {
	// 创建db
	db := global.GVA_DB.Model(&area.LsArea{})
	var lsAreas []area.LsArea
	err := db.Where("level_num=?", 0).Find(&lsAreas).Error
	for k := range lsAreas {
		lsSupplierService.FindChildren(&lsAreas[k])
	}
	return lsAreas, err
}

func (lsSupplierService *LsSupplierService) FindChildren(class *area.LsArea) error {
	err := global.GVA_DB.Where("pid=?", class.ID).Find(&class.Children).Error
	if len(class.Children) > 0 {
		for k := range class.Children {
			err = lsSupplierService.FindChildren(&class.Children[k])
		}
	}
	return err
}
func (lsSupplierService *LsSupplierService) GetLsSuppliersInfoList() (list []supplier.LsSupplier, err error) {
	// 创建db
	db := global.GVA_DB.Model(&supplier.LsSupplier{})
	var lsSuppier []supplier.LsSupplier
	// 如果有条件搜索 下方会自动创建搜索语句
	//if lsBrandService.StartCreatedAt != nil && info.EndCreatedAt != nil {
	//	db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	//}

	err = db.Find(&lsSuppier).Error
	return lsSuppier, err
}
